﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using shop.entity;
using shop.dao;
using shop.exception;
using shop.constant;
using shop.util;

namespace shop.service
{
    public class UserService
    {
        private UserDao userDao = new UserDao();
        public User getUser(int id)
        {
            return userDao.GetUser(id);
        }

        public List<User> ListUser(User user)
        {
            return userDao.ListUser(user);
        }

        public bool Register(User user, string rePass)
        {
            //默认注册为普通用户
            user.Rank = (int)UserRankEnum.USER_ROLE;
            return Add(user, rePass);
        }

        public bool Add(User user, string rePass)
        {
            if (StringUtil.IsBalnk(user.Name))
            {
                throw new ShopParamException("用户名不能为空！");
            }
            if (user.Rank == 0)
            {
                //默认注册为普通用户
                user.Rank = (int)UserRankEnum.USER_ROLE;
            }
            bool exist = UsernameExist(user.Name);
            if (exist)
            {
                throw new ShopParamException("用户名已经被其他人注册了！");
            }

            if (StringUtil.IsBalnk(user.Password))
            {
                throw new ShopParamException("密码不能为空！");
            }
            if (user.Password.Length < 6)
            {
                throw new ShopParamException("密码至少6位！");
            }
            if (!user.Password.Equals(rePass.Trim()))
            {
                throw new ShopParamException("两次输入密码不一致！");
            }

            return userDao.AddUser(user);
        }

        public bool UsernameExist(string name)
        {
            User loginUser = userDao.GetUserByName(name);
            if (loginUser != null)
            {
                return true;
            }
            return false;
        }

        public User Login(User user)
        {
           
            if (StringUtil.IsBalnk(user.Name))
            {
                throw new ShopParamException("用户名不能为空！");
            }
            User loginUser = userDao.GetUserByName(user.Name);
            if (loginUser == null || loginUser.Name==null)
            {
                throw new ShopParamException("用户名不存在！");

            }
            else if (!loginUser.Password.Equals(user.Password))
            {
                throw new ShopParamException("用户名或者密码错误！");
            }
            
            return loginUser;

        }
        public bool DeleteUser(int id)
        {
            return userDao.DeleteUser(id);
        }

        public bool UpdateUser(User user, string rePassword)
        {
            if (StringUtil.IsBalnk(user.Name))
            {
                throw new ShopParamException("用户名不能为空！");
            }
            if (StringUtil.IsBalnk(user.Password))
            {
                throw new ShopParamException("密码不能为空！");
            }
            if (user.Password.Length < 6)
            {
                throw new ShopParamException("密码至少6位！");
            }

            if (!user.Password.Trim().Equals(rePassword.Trim()))
            {
                throw new ShopParamException("两次输入密码不一致");
            }
            if (!user.Password.Trim().Equals(rePassword.Trim()))
            {
                throw new ShopParamException("两次输入密码不一致");
            }
            if (user.Rank==0)
            {
                throw new ShopParamException("选择用户角色");
            }
            User checkedUser = userDao.GetUserByName(user.Name);
            if (user != null && user.Name.Equals(checkedUser.Name) && user.Id != checkedUser.Id)
            {
                throw new ShopParamException("用户名已经被其他人使用了了！");
            }
            if (StringUtil.IsBalnk(user.Name))
            {
                throw new ShopParamException("");
            }
            return userDao.UpdateUser(user);
        }
    }
}